# 1.导包
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression # 逻辑回归
from sklearn.metrics import accuracy_score              # 因为逻辑回归实际是解决分类问题的，因此准确度依然还可以用

# 2.读取数据
df = pd.read_csv("breast-cancer-wisconsin.csv", encoding="UTF-8")

# 3.数据的基本处理
# 3.1 将?替换成NAN
df.replace('?', np.NAN, inplace=True)

# 3.2 删除空行
df.dropna(inplace=True)

x = df.iloc[:, 1:-1]
y = df.iloc[:, -1]

# 3.3 数据划分
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=922)

# 4.特征工程
# 4.1 特征预处理(标准化特征值)
transformer = StandardScaler()
x_train = transformer.fit_transform(x_train)     # 对特征数据进行学习、处理转换

# 5.机器学习(模型构建和训练)
model = LogisticRegression()    # 构建算法实例对象
model.fit(x_train, y_train)     # 使用训练集进行训练

# 6.模型评估
# 6.1 对测试集进行标准化
x_test = transformer.transform(x_test)

# 6.2 模型评估
result = model.predict(x_test)      # 根据测试集对模型进行评估
print(f"模型的准确率为：{accuracy_score(y_test, result)}")